package com.example.computer_exam_training_sys.controller;

import com.example.computer_exam_training_sys.entity.ExamPaper;
import com.example.computer_exam_training_sys.service.ExamPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/exam-papers")
@CrossOrigin // 允许跨域请求，开发环境中必须开启
public class ExamPaperController {

    @Autowired
    private ExamPaperService examPaperService;

    /**
     * ✅ 查询所有试卷
     * 示例请求：GET http://localhost:8080/exam-papers/list/all
     */
    @GetMapping("/list/all")
    public List<ExamPaper> getAllExamPapers() {
        return examPaperService.getAllExamPapers();
    }

    /**
     * ✅ 根据试卷ID查询试卷详情
     * 示例请求：GET http://localhost:8080/exam-papers/1
     */
    @GetMapping("/{paperId}")
    public ExamPaper getPaperById(@PathVariable Integer paperId) {
        return examPaperService.getPaperById(paperId);
    }

    /**
     * ✅ 新增试卷
     * 示例请求：POST http://localhost:8080/exam-papers/add
     * 请求体传入 ExamPaper JSON 对象
     */
    @PostMapping("/add")
    public ExamPaper addExamPaper(@RequestBody ExamPaper examPaper) {
        return examPaperService.addExamPaper(examPaper);
    }

    /**
     * ✅ 修改试卷
     * 示例请求：PUT http://localhost:8080/exam-papers/update/1
     * 请求体传入修改后的 ExamPaper JSON 对象
     */
    @PutMapping("/update/{paperId}")
    public ExamPaper updateExamPaper(@PathVariable Integer paperId, @RequestBody ExamPaper examPaper) {
        examPaper.setPaperId(paperId); // 确保更新时设置ID
        return examPaperService.updateExamPaper(examPaper);
    }

    /**
     * ✅ 删除试卷
     * 示例请求：DELETE http://localhost:8080/exam-papers/del/1
     */
    @DeleteMapping("/del/{paperId}")
    public int deleteUser(@PathVariable("paperId") int paperId) {
        return examPaperService.deleteUser(paperId);
    }
}
